using System;
using System.Collections.Generic;
using iPhOwa.Exchange.ContentClasses;
using iPhOwa.Exchange.ContentClasses.Fields;
using iPhOwa.Exchange.WebDav.Methods;

namespace iPhOwa.Exchange.Managers
{
	/// <summary>
	/// Encapsulates working with folders.
	/// </summary>
	public static class FolderManager
	{
		/// <summary>
		/// Get a single folder.
		/// </summary>
		public static Folder Get(Uri path, OwaSession session)
		{
			string[] properties = new string[]
					{
							ItemFields.Href
							, ItemFields.ParentName
							, ItemFields.DisplayName
							, FolderFields.UnreadCount
					};

			PropFind currentFolderPropFind = new PropFind(path, session, properties);

			return currentFolderPropFind.Run() as Folder;
		}

		/// <summary>
		/// Get a list of folders.
		/// </summary>
		/// <returns></returns>
		public static ICollection<Folder> GetList(Uri path, OwaSession session)
		{
			string[] properties = new string[]
					{
							ItemFields.Read
							, ItemFields.Href
							, ItemFields.ParentName
							, ItemFields.DisplayName
							, FolderFields.UnreadCount
							, ItemFields.HasSubs
					};

			Search search = new Search(path, session, properties);
			search.IsFolder = true;

			List<Item> items = new List<Item>(search.Run());

			return items.ConvertAll<Folder>(delegate(Item i)
			                                	{
			                                		return i as Folder;
			                                	});
		}
	}
}